package Day14_ArrayList;

//创建一个存储学生对象的集合,遍历
//1.创建学生类
//2.创建集合
//3.创建3个学生对象
//4.添加
//5.遍历

import java.util.ArrayList;

public class ArrayList集合遍历学生类 {
    public static void main(String[] args) {
        //2.创建集合
        ArrayList<student> list = new ArrayList<>();
        //3.创建学生对象
        student s1 = new student("马云", 40);   //s1 = 1540e19d
        student s2 = new student("马化腾", 41); //s2 = 677327b6
        student s3 = new student("刘强东", 42); //s3 = 14ae5a5
        System.out.println("s1=" + s1); //1540e19d
        System.out.println("s2=" + s2); //677327b6
        System.out.println("s3=" + s3); //14ae5a5

        //4.添加到集合中  添加的是地址!!!!!
        list.add(s1); //1540e19d
        list.add(s2); //677327b6
        list.add(s3); //14ae5a5

        //5.遍历
        for (int i = 0; i < list.size(); i++) {
            //i = 0,1,2
            //i = 0;list.get(0)-->1540e19d
            //i = 0;list.get(1)-->677327b6
            //i = 0;list.get(2)-->14ae5a5
            //取出的是对象地址
            //System.out.println(list.get(i) + " ");

            //定义变量保存地址
            student s = list.get(i);
            System.out.println(s.getName() + " " + s.getAge());
        }
    }
}

//----------------------------------------------------------------------------------------------------------------------

//自己通过class定义的类,属于引用数据类型
//1.定义学生类
class student {
    private String name;
    private int age;

    public student() {

    }

    public student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
